using System;
using System.Net;
using CodePlex.TfsLibrary.RegistrationWebSvc;
using CodePlex.UnitTestUtility;
using Xunit;

namespace CodePlex.TfsLibrary.ObjectModel
{
    public class RegistrationServiceTest
    {
        [Fact]
        public void CanRetrieveServiceInterfaceUrl()
        {
            RegistrationService svc = new RegistrationService(new Factory());

            string url = svc.GetServiceInterfaceUrl("http://www.dummy.com:8080", null, "Foo", "Bar");

            Assert.Equal("http://www.dummy.com:8080/Fooble.asmx", url);
        }

        [Fact]
        public void TrailingSlashOnUrlIsOkay()
        {
            RegistrationService svc = new RegistrationService(new Factory());

            string url = svc.GetServiceInterfaceUrl("http://www.dummy.com:8080/", null, "Foo", "Bar");

            Assert.Equal("http://www.dummy.com:8080/Fooble.asmx", url);
        }

        [Fact]
        public void UnknownInterfaceForKnownServiceThrows()
        {
            RegistrationService svc = new RegistrationService(new Factory());

            Assert.Throws<ArgumentException>(delegate
                                             {
                                                 svc.GetServiceInterfaceUrl("http://www.dummy.com:8080", null, "Foo", "Baz");
                                             });
        }

        [Fact]
        public void UnknownServiceThrows()
        {
            RegistrationService svc = new RegistrationService(new Factory());

            Assert.Throws<ArgumentException>(delegate
                                             {
                                                 svc.GetServiceInterfaceUrl("http://www.dummy.com:8080", null, "Baz", "Bar");
                                             });
        }

        class Factory : IRegistrationWebSvcFactory
        {
            public IRegistrationWebSvc Create(string tfsUrl,
                                              ICredentials credentials)
            {
                return new StubRegistrationWebSvc();
            }
        }
    }
}